package com.risen.project.controller;

import com.alibaba.excel.EasyExcel;
import com.risen.project.annotation.LogMsg;
import com.risen.project.common.Response;
import com.risen.project.common.ResponseStatusCode;
import com.risen.project.domain.dto.ProjectManageDTO;
import com.risen.project.domain.vo.ProjectManageVO;
import com.risen.project.service.ProjectManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/project")
@Api(tags = "项目管理API")
public class ProjectManageController {

    @Resource
    private ProjectManageService projectManageService;

    @GetMapping("/projectSend")
    @ApiOperation("项目信息上报接口")
    @LogMsg("项目信息上报接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "baseProjIds", value = "项目IDS 该参数未空的话 则全部上报 不为空则只上报指定的项目信息（多个英文逗号隔开）")
    })
    public Response<Map<String, Integer>> projectSend(String baseProjIds) {
        return Response.success(projectManageService.sendProjectManage(baseProjIds));
    }

    @PostMapping("/insertProjectManage")
    @ApiOperation("插入项目管理信息表接口")
    @LogMsg("插入项目管理信息表接口")
    public Response<Integer> insertProjectManage(@Validated @RequestBody ProjectManageDTO data) {
        return Response.success(projectManageService.insertProjectManage(data));
    }


    @PostMapping("/updateProjectManage")
    @ApiOperation("更新项目信息接口")
    @LogMsg("更新项目信息接口")
    public Response<Integer> updateProjectManage(@Validated @RequestBody ProjectManageDTO data) {
        return Response.success(projectManageService.updateProjectManage(data));
    }

    @GetMapping("/downloadExcelTemplate")
    @ApiOperation("下载表格模板接口")
    @LogMsg("下载表格模板接口")
    public Response<Void> downloadExcelTemplate(HttpServletResponse response) {

        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=template" + ".xlsx");
        List<ProjectManageDTO> excelHeads = new ArrayList<>();
        try {
            EasyExcel.write(response.getOutputStream(), ProjectManageDTO.class).useDefaultStyle(false).sheet().doWrite(excelHeads);
        } catch (IOException e) {
            e.printStackTrace();
            return Response.fail(ResponseStatusCode.FAIL.getCode(), "采集点模板导出失败");
        }
        return Response.success();
    }

    @PostMapping("/importProjectManageByExcel")
    @ApiOperation("导入项目管理信息接口")
    @LogMsg("导入项目管理信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件对象", required = true)
    })
    public Response<String> importProjectManageByExcel(MultipartFile file) {
        return Response.success(projectManageService.importProjectManageByExcel(file));
    }

    @GetMapping("/exportProjectManage")
    @ApiOperation("导出项目信息接口")
    @LogMsg("导出项目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "baseProjName", value = "项目名称"),
            @ApiImplicitParam(name = "baseApplyDept", value = "申报单位"),
            @ApiImplicitParam(name = "createdAt", value = "申报时间"),
            @ApiImplicitParam(name = "reportStatus", value = "上报状态 0-未上报 1-已上报 2-上报失败")
    })
    public Response<Void> exportProjectManage(
            @RequestParam(required = false) String baseProjName,
            @RequestParam(required = false) String baseApplyDept,
            @RequestParam(required = false) String createdAt,
            @RequestParam(required = false) Integer reportStatus,
            HttpServletResponse response) {
        projectManageService.exportProjectManage(baseProjName, baseApplyDept, createdAt, reportStatus, response);
        return Response.success();
    }

    @PostMapping("/getProjectList")
    @ApiOperation("获取项目信息列表接口")
    @LogMsg("获取项目信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "baseProjName", value = "项目名称"),
            @ApiImplicitParam(name = "baseApplyDept", value = "申报单位"),
            @ApiImplicitParam(name = "createdAt", value = "申报时间"),
            @ApiImplicitParam(name = "reportStatus", value = "上报状态 0-未上报 1-已上报 2-上报失败"),
            @ApiImplicitParam(name = "page", value = "页", required = true),
            @ApiImplicitParam(name = "size", value = "大小")
    })
    public Response<List<ProjectManageVO>> getProjectList(
            @RequestParam(required = false) String baseProjName,
            @RequestParam(required = false) String baseApplyDept,
            @RequestParam(required = false) String createdAt,
            @RequestParam Integer reportStatus,
            @RequestParam int page,
            @RequestParam(required = false, defaultValue = "10") int size) {
        return projectManageService.getProjectList(baseProjName, baseApplyDept, createdAt, reportStatus, page, size);
    }
}
